import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

@Injectable()
export class AllInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const res = context.switchToHttp().getResponse()
    // res.header(
    //   'Cache-Control',
    //   'no-cache, no-store, max-age=0, must-revalidate',
    // )
    // res.header('Pragma', 'no-cache')
    if (res.statusCode >= 200 && res.statusCode < 300) {
      res.statusCode = 200
    }
    return next.handle().pipe(
      map(data => {
        return {
          code: res.statusCode,
          message: '请求成功',
          data,
        }
      }),
    )
  }
}
